<?php
/**
 * DB interations for blogs and comments
 *  
 * author: Chris Lunsford
 **/
class BlogModel extends Model {
	
	function __construct() {
		parent::__construct();
	
	}
	
	function createPost($post) {
		$this->db->insert('blog_posts', $post);
	}
	
	function deletePost($id) {
		$this->db->where('id', $id);
		$this->db->delete('blog_posts');
		
		//also delete all comments for this post;
		$comments = $this->getComments($id);
		if ($comments->num_rows() > 0) {
			foreach ($comments->result() as $row) {
				$this->deleteComment($row->id);
			}
		}
	}
	
	function updatePost($id, $post) {
		$this->db->where('id', $id);
		$this->db->update('blog_posts', $post);
	}
	
	function getPost($id) {
		$this->db->where('id', $id);
		return $this->db->get('blog_posts')->row();
	}
	
	function getPosts($public = 'all', $published = 'all', $limit = FALSE) {
		if ($public == 'public') {
			$this->db->where('public','1');
		} elseif ($public == 'private') {
			$this->db->where('public','0');
		}
		
		if ($published == 'published') {
			$this->db->where('published','1');
		} elseif ($public == 'draft') {
			$this->db->where('published','0');
		}
		
		if ($limit != FALSE) {
			$this->db->limit($limit);
		}
		$this->db->orderby('posted', 'desc');
		return $this->db->get('blog_posts');
	}
	
	function getRecentPosts() {
		$this->db->orderby('posted', 'desc');
		$this->db->where('public','1');
		$this->db->where('published','1');
		$this->db->limit(10);		
		return $this->db->get('blog_posts');
	}
	
	function createComment($post) {
		$this->db->insert('blog_comments',$post);
	}
	
	function deleteComment($id) {
		$this->db->where('id',$id);
		$this->db->delete('blog_comments');
	}
	
	function updateComment($id, $post) {
		$this->db->where('id',$id);
		$this->db->update('blog_comments',$post);
	}
	
	function getComment($id) {
		$this->db->where('id', $id);
		return $this->db->get('blog_comments')->row();
	}
	
	function getComments($postid, $approved = "all") {
		if($approved == "approved") $this->db->where('approved','y');
		$this->db->where('postid', $postid);
		return $this->db->get('blog_comments');
	}

}
?>
